home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / GLUTFX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-29  |  4.2 KB  |  198 lines

  1. /* $Id: glutfx.c,v 3.1 1998/06/29 02:37:30 brianp Exp $ */
  2.  
  3. /*
  4.  * Example of how one might use GLUT with the 3Dfx driver in full-screen mode.
  5.  * Note: this only works with X since we're using Mesa's GLX "hack" for
  6.  * using Glide.
  7.  *
  8.  * Goals:
  9.  *   easy setup and input event handling with GLUT
  10.  *   use 3Dfx hardware
  11.  *   automatically set MESA environment variables
  12.  *   don't lose mouse input focus
  13.  *
  14.  * Brian Paul   This file is in the public domain.
  15.  */
  16.  
  17. /*
  18.  * $Log: glutfx.c,v $
  19.  * Revision 3.1  1998/06/29 02:37:30  brianp
  20.  * minor changes for Windows (Ted Jump)
  21.  *
  22.  * Revision 3.0  1998/02/14 18:42:29  brianp
  23.  * initial rev
  24.  *
  25.  */
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <math.h>
  31. #include <GL/glut.h>
  32.  
  33.  
  34. #define WIDTH 640
  35. #define HEIGHT 480
  36.  
  37.  
  38. static int Window = 0;
  39. static int ScreenWidth, ScreenHeight;
  40. static GLuint Torus = 0;
  41. static GLfloat Xrot = 0.0, Yrot = 0.0;
  42.  
  43.  
  44.  
  45. static void Display( void )
  46. {
  47.    static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
  48.    static GLfloat red[4] = {1.0, 0.2, 0.2, 1.0};
  49.    static GLfloat green[4] = {0.2, 1.0, 0.2, 1.0};
  50.  
  51.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  52.  
  53.    glPushMatrix();
  54.    glRotatef(Xrot, 1, 0, 0);
  55.    glRotatef(Yrot, 0, 1, 0);
  56.  
  57.    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
  58.    glCallList(Torus);
  59.  
  60.    glRotatef(90.0, 1, 0, 0);
  61.    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
  62.    glCallList(Torus);
  63.  
  64.    glRotatef(90.0, 0, 1, 0);
  65.    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green);
  66.    glCallList(Torus);
  67.  
  68.    glPopMatrix();
  69.  
  70.    glutSwapBuffers();
  71. }
  72.  
  73.  
  74. static void Reshape( int width, int height )
  75. {
  76.    float ratio = (float) width / (float) height;
  77.  
  78.    ScreenWidth = width;
  79.    ScreenHeight = height;
  80.  
  81.    /*
  82.     * The 3Dfx driver is limited to 640 x 480 but the X window may be larger.
  83.     * Enforce that here.
  84.     */
  85.    if (width > WIDTH)
  86.       width = WIDTH;
  87.    if (height > HEIGHT)
  88.       height = HEIGHT;
  89.  
  90.    glViewport( 0, 0, width, height );
  91.    glMatrixMode( GL_PROJECTION );
  92.    glLoadIdentity();
  93.    glFrustum( -ratio, ratio, -1.0, 1.0, 5.0, 30.0 );
  94.    glMatrixMode( GL_MODELVIEW );
  95.    glLoadIdentity();
  96.    glTranslatef( 0.0, 0.0, -20.0 );
  97. }
  98.  
  99.  
  100. static void Key( unsigned char key, int x, int y )
  101. {
  102.    switch (key) {
  103.       case 27:
  104.          glutDestroyWindow(Window);
  105.          exit(0);
  106.          break;
  107.    }
  108.    glutPostRedisplay();
  109. }
  110.  
  111.  
  112. static void SpecialKey( int key, int x, int y )
  113. {
  114.    switch (key) {
  115.       case GLUT_KEY_UP:
  116.          break;
  117.       case GLUT_KEY_DOWN:
  118.          break;
  119.       case GLUT_KEY_LEFT:
  120.          break;
  121.       case GLUT_KEY_RIGHT:
  122.          break;
  123.    }
  124.    glutPostRedisplay();
  125. }
  126.  
  127.  
  128. static void MouseMove( int x, int y )
  129. {
  130.    Xrot = y - ScreenWidth / 2;
  131.    Yrot = x - ScreenHeight / 2;
  132.    glutPostRedisplay();
  133. }
  134.  
  135.  
  136. static void Init( void )
  137. {
  138.    Torus = glGenLists(1);
  139.    glNewList(Torus, GL_COMPILE);
  140.    glutSolidTorus(0.5, 2.0, 10, 20);
  141.    glEndList();
  142.  
  143.    glEnable(GL_LIGHTING);
  144.    glEnable(GL_LIGHT0);
  145.  
  146.    glEnable(GL_DEPTH_TEST);
  147.    glEnable(GL_CULL_FACE);
  148. }
  149.  
  150.  
  151. int main( int argc, char *argv[] )
  152. {
  153. #ifndef _WIN32
  154.    printf("NOTE: if you've got 3Dfx VooDoo hardware you must run this");
  155.    printf(" program as root.\n\n");
  156.    printf("Move the mouse.  Press ESC to exit.\n\n");
  157.    sleep(2);
  158. #endif
  159.  
  160.    /* Tell Mesa GLX to use 3Dfx driver in fullscreen mode. */
  161.    putenv("MESA_GLX_FX=fullscreen");
  162.  
  163.    /* Disable 3Dfx Glide splash screen */
  164.    putenv("FX_GLIDE_NO_SPLASH=");
  165.  
  166.    /* Give an initial size and position so user doesn't have to place window */
  167.    glutInitWindowPosition(0, 0);
  168.    glutInitWindowSize(WIDTH, HEIGHT);
  169.    glutInit( &argc, argv );
  170.  
  171.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  172.  
  173.    Window = glutCreateWindow(argv[0]);
  174.    if (!Window) {
  175.       printf("Error, couldn't open window\n");
  176.       exit(1);
  177.    }
  178.  
  179.    /*
  180.     * Want the X window to fill the screen so that we don't have to
  181.     * worry about losing the mouse input focus.
  182.     * Note that we won't actually see the X window since we never draw
  183.     * to it, hence, the original X screen's contents aren't disturbed.
  184.     */
  185.    glutFullScreen();
  186.  
  187.    Init();
  188.  
  189.    glutReshapeFunc( Reshape );
  190.    glutKeyboardFunc( Key );
  191.    glutSpecialFunc( SpecialKey );
  192.    glutDisplayFunc( Display );
  193.    glutPassiveMotionFunc( MouseMove );
  194.  
  195.    glutMainLoop();
  196.    return 0;
  197. }
  198.